﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;

namespace FourKill.Engine
{
    public class FlyingBirdObject : BaseGameObject
    {
        private const int BOUNDRY_SIZE = 2000;
        private Sprite _sprite;
        private RectangleF _boundries = RectangleF.Empty;
        private int _moveAwayCounter = 0;

        public FlyingBirdObject()
        {
            Speed = 1+_rand.NextDouble() * 1;
            Z = _rand.Next(30, 200);
        }

        public override void Tick()
        {
            if (_boundries == RectangleF.Empty)
            {
                _boundries = new RectangleF(Convert.ToSingle(this.X) - BOUNDRY_SIZE, Convert.ToSingle(this.Y) - BOUNDRY_SIZE, BOUNDRY_SIZE * 2, BOUNDRY_SIZE * 2);
                _moveAwayCounter = 0;
            }
            else
            {
                if (!_boundries.Contains(this.PointF))
                {
                    if (_moveAwayCounter == 0)
                    {
                        this.Turn(Utility.Radians(_rand.Next(150, 210)));
                        _moveAwayCounter = _rand.Next(3, 6);
                    }
                }
                else
                {
                    _moveAwayCounter = 0;
                }
            }
            if (_moveAwayCounter == 0)
            {
                var r = _rand.Next(50);
                if (r < 10)
                {
                    if (Z > 200) Z += _rand.Next(-2, 0);
                    else if (Z < 20) Z += _rand.Next(0, 2);
                    else Z += _rand.Next(-1, 1);
                }
                if (r == 25) Speed = 1 + _rand.NextDouble() * 1;
                if (r == 45) this.Turn(Utility.Radians(_rand.Next(-30, 30)));
                else this.Turn(Utility.Radians(_rand.Next(-2, 2)));
            }
            else
            {
                _moveAwayCounter--;
            }
            this.Move(Speed);
        }

        public Sprite Sprite
        {
            get 
            {
                _sprite = _sprite ?? Sprite.Instance(SpriteSheets.Bird01);
                return _sprite;
            }
        }

        public override IPaintable Painter
        {
            get
            {
                return Sprite;
            }
        }
    }
}
